package com.tpshion.cloud.auth.utils;

import com.tpshion.cloud.auth.domain.entity.Menu;
import com.tpshion.cloud.auth.utils.support.MenuData;
import com.tpshion.cloud.auth.utils.support.MenuTree;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单工具类
 */
public class MenuUtil {

    /**
     * 获取子菜单
     * @param id 菜单id
     * @param menus 用户拥有菜单列表
     * @return
     */
    public static List<MenuTree> getChildren(String id, List<Menu> menus){
        List<MenuTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(!id.equals(menu.getParentId())){
                continue;
            }
            MenuTree menuTree = new MenuTree();
            BeanUtils.copyProperties(menu,menuTree);
            List<MenuTree> children = getChildren(menu.getId(), menus);
            menuTree.setChildren(children.isEmpty() ? null : children);
            list.add(menuTree);
        }
        return list;
    }

    /**
     * 生成用户菜单树
     * @param menus 用户拥有的菜单列表
     * @return
     */
    public static List<MenuTree> getMenuTree(List<Menu> menus){
        List<MenuTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(!StringUtils.isEmpty(menu.getParentId())){
                continue;
            }
            MenuTree menuTree = new MenuTree();
            BeanUtils.copyProperties(menu,menuTree);
            List<MenuTree> children = getChildren(menu.getId(),menus);
            menuTree.setChildren(children.isEmpty() ? null : children);
            list.add(menuTree);
        }
        return list;
    }

    /**
     * 生成指定角色的菜单树数据
     */
    public static List<MenuData> getMenuData(List<Menu> menus, List<String> menuIds){
        List<MenuData> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(!StringUtils.isEmpty(menu.getParentId())){
                continue;
            }
            MenuData menuData = new MenuData();
            menuData.setId(menu.getId());
            menuData.setTitle(menu.getTitle());
            menuData.setExpand(true);
            menuData.setIcon(menu.getIcon());
            menuData.setSysCode(menu.getSysCode());
            menuData.setName(menu.getName());
            if(menuIds.contains(menuData.getId())){
                menuData.setChecked(true);
            }
            List<MenuData> children = getMenuDataChildren(menu.getId(),menus,menuIds);
            menuData.setChildren(children.isEmpty() ? null : children);
            list.add(menuData);
        }
        return list;
    }

    public static List<MenuData> getMenuDataChildren(String id, List<Menu> menus, List<String> menuIds){
        List<MenuData> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(!id.equals(menu.getParentId())){
                continue;
            }
            MenuData menuData = new MenuData();
            menuData.setId(menu.getId());
            menuData.setTitle(menu.getTitle());
            menuData.setExpand(true);
            menuData.setIcon(menu.getIcon());
            menuData.setSysCode(menu.getSysCode());
            menuData.setName(menu.getName());
            if(menuIds.contains(menuData.getId())){
                menuData.setChecked(true);
            }
            List<MenuData> children = getMenuDataChildren(menu.getId(), menus, menuIds);
            menuData.setChildren(children.isEmpty() ? null : children);
            list.add(menuData);
        }
        return list;
    }

    /**
     * 生成所有菜单数据的菜单树
     * @param menus
     * @return
     */
    public static List<MenuData> getAllMenuData(List<Menu> menus){
        List<MenuData> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(!StringUtils.isEmpty(menu.getParentId())){
                continue;
            }
            MenuData menuData = new MenuData();
            menuData.setId(menu.getId());
            menuData.setTitle(menu.getTitle());
            menuData.setExpand(true);
            menuData.setIcon(menu.getIcon());
            menuData.setSysCode(menu.getSysCode());
            menuData.setName(menu.getName());
            List<MenuData> children = getAllMenuDataChildren(menu.getId(),menus);
            menuData.setChildren(children.isEmpty() ? null : children);
            list.add(menuData);
        }
        return list;
    }

    public static List<MenuData> getAllMenuDataChildren(String id, List<Menu> menus){
        List<MenuData> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(!id.equals(menu.getParentId())){
                continue;
            }
            MenuData menuData = new MenuData();
            menuData.setId(menu.getId());
            menuData.setTitle(menu.getTitle());
            menuData.setExpand(true);
            menuData.setIcon(menu.getIcon());
            menuData.setSysCode(menu.getSysCode());
            menuData.setName(menu.getName());
            List<MenuData> children = getAllMenuDataChildren(menu.getId(), menus);
            menuData.setChildren(children.isEmpty() ? null : children);
            list.add(menuData);
        }
        return list;
    }
}
